package action;

import java.util.Map;

import model.Assessor;
import model.User;
import action.base.BaseAction;

import com.opensymphony.xwork2.ActionContext;
public class LoginAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 48L;
	private String user;
	private String pass;
	private String vercode;
	private int power;
	private String tip;
	
	public int getPower() {
		return power;
	}
	public void setPower(int power) {
		this.power = power;
	}
	public String getTip() {
		return tip;
	}
	public void setTip(String tip) {
		this.tip = tip;
	}
	public String getUser() {
		return user;
	}
	public void setUser(String user) {
		this.user = user;
	}
	public String getPass() {
		return pass;
	}
	public void setPass(String pass) {
		this.pass = pass;
	}
	public String getVercode() {
		return vercode;
	}
	public void setVercode(String vercode) {
		this.vercode = vercode;
	}
	public String execute() throws Exception{
		Map session = ActionContext.getContext().getSession();
		String ver = (String)session.get("rand");
		session.put("rand", null);
		int userId;
		if(vercode.equals(ver)){
			if(power == 2){
				userId = mga.loginValid(user,pass);
				if(userId > 0){
					Assessor a = mga.getUser(userId);
					if(power == a.getPower()){
						session.put("ASSESSOR", a);
						session.put("assessorId", userId);
						return "assessor";
					}else{
						setTip("权限不够");
					}
				}else{
					setTip("用户名或密码不正确");
				}
			}else{
				userId = mgr.loginValid(user,pass);
				if(userId > 0){
					User u = mgr.getUser(userId);
					if(power == u.getPower()){
						session.put("USER", u);
						session.put("userId", userId);
							if(power == 1){
								return "admin";
							}
						return "success";
					}else{
						setTip("权限不够");
					}
				}else{
					setTip("用户名或密码不正确");
				}
			}
		}else{
			setTip("验证码不匹配");
		}
		return "failure";
		
		 
	}

}
